from db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import time

'''
基金十大持仓数据
'''

entity_gupiao = "0"
entity_jijin = "1"

class ChiCang:
    def __init__(self, start, end):
        self.db = MysqlHander("config.ini")
        self.start = start
        self.end = end
        
    def insert_jijin_gupiao(self, gupiao, order_tag):
        #基金 - 股票
        sql1 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Forder_tag, Fcreate_time, Fmodify_time) values "
        sql1 += "('" + self.jijin + "'," + entity_jijin + ",1002,'" + gupiao + "'," + entity_gupiao + ", " + str(order_tag) + ",now(), now());"
        print(sql1)
        try:
            #pass
            self.db.execute_not_safe(sql1)
        except Exception as e:
            print(str(e))
            pass
            
        #股票 - 基金
        sql2 = "insert into relation_data_01 (Fsrc_code, Fsrc_type, Frelation_id, Fdst_code, Fdst_type, Forder_tag, Fcreate_time, Fmodify_time) values "
        sql2 += "('" + gupiao + "'," + entity_gupiao + ",1001,'" + self.jijin + "'," + entity_jijin + ", " + str(order_tag) + ", now(), now());"
        print(sql2)
        try:
            #pass
            self.db.execute_not_safe(sql2)
        except Exception as e:
            print(str(e))
            pass
        #print(str(res))
    def get_http_data(self, code):
        self.jijin = code
        url = "http://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=" + code + "&topline=10&year=&month=&rt=0.26772014601735106"
        http_data = MyHttp.http_json_data(url)
        fileds = http_data.decode().split('"')
        #print(fileds[1])
        if len(fileds[1]) == 0:
            return
        soup = BeautifulSoup(fileds[1], "html5lib")

        d = soup.find(name="div", attrs={'class':'boxitem w790'})
        #print(d)
        trs = d.find_all("tr")
        order_tag = 1
        for tr in trs:
            tds = tr.find_all("td")
            if len(tds) > 1:
                self.insert_jijin_gupiao(tds[1].text, order_tag)
            ++order_tag
                
    def query_codes(self):
        sql = "select Fcode from entity_jijin where Fid >= " + str(self.start) + " and Fid < " + str(self.end) + ";"
        self.fund_codes = self.db.query_sql(sql)
    
    def get_data(self):
        self.query_codes()
        for fund in self.fund_codes:
            self.get_http_data(fund[0])
            time.sleep(3)

'''
class MyThread(threading.Thread):
    def __init__(self, start, end):
        self.start = start
        self.end = end
    def run(self):
        d = ChiCang(self.start, self.end)
        d.get_data()
        
if __name__ == '__main__':
    for i in range(0, 15):
        t = MyThread(i * 1000, (i + 1)*1000)
        t.start()
        t.join()
'''        

def my_thread(start, end):
    d = ChiCang(start, end)
    d.get_data()

if __name__ == '__main__':
    threads = []
    for i in range(0, 15):
        t = threading.Thread(target=my_thread, args=(i * 1000, (i + 1)*1000))
        threads.append(t)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
